package com.douwong.student.application;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.support.multidex.MultiDex;

import com.douwong.student.model.FtpInfo;
import com.douwong.student.model.StudentAtt;
import com.douwong.student.model.StudentOnline;
import com.douwong.student.model.TeacherOnline;
import com.douwong.student.model.UserBean;
import com.douwong.student.utils.AppCacheManager;
import com.douwong.student.utils.CrashHandler;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import org.xutils.x;

import java.io.IOException;

/**
 * @autor yichao
 * @time 2017/3/2115:39
 * @des ${TODO}
 */
public class BaseApplication  extends Application{
    public static Context mContext;
    public static Handler mHandler;
    public static long		mMainThreadId;
    public static Thread	mMainThread;
    public static UserBean sUserBean; //登录信息
    public static boolean isLogin ; //是否登录
    public  static  String AppKeyPWD="zxcv6789";
    public static  boolean isLoginClass =false;//是否加入课堂成功
    public static StudentOnline sStudentOnline; //在线学生信息
    public static TeacherOnline sTeacherOnline ;//加入课堂老师信息
    public static TeacherOnline temTeacherOnline;//缓存老师信息
    public static FtpInfo sFtpInfo;
    public static boolean isCloseConn; //是否关闭长连接
    public static boolean isCancelConn; //是否取消长连接
    public static int noDoTestSum   ;//没做试题的总数
    public static boolean isTeaching;  //老师是否在线
    public static boolean isTimeControl;
    public static StudentAtt sStudentAtt;//学生属性
    public static long currentHeartTime;//检测当前心跳时间



    @Override
    public void onCreate() {
        super.onCreate();
       /* if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);*/

        x.Ext.init(this);
        x.Ext.setDebug(false); //是否输出debug日志，开启debug会影响性能。
        initImageLoader(this);
        // 1.上下文
        mContext = getApplicationContext();
        // 2.创建一个handler
        mHandler = new Handler();
        // 3.得到一个主线程id
        mMainThreadId = android.os.Process.myTid();
        // 4.得到主线程
        mMainThread = Thread.currentThread();
        // 错误日志
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        Fresco.initialize(mContext);

    }
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    /**
     * 保存对象
     *
     * @param ser  对象数据
     * @param file 对象key(存放名称)
     * @throws IOException
     */
    public synchronized boolean saveObject(Object ser, String file) {
        return AppCacheManager.saveObject(this,ser,file);
    }

    /**
     * 读取对象
     *
     * @param file
     * @return
     * @throws IOException
     */
    public Object readObject(String file) {
        return AppCacheManager.readObject(this,file);
    }
    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());

    }

}
